/**
 * 
 */

package com.kugou.game.sdk.ui.fragment;

import com.kugou.framework.utils.NetWorkUtil;
import com.kugou.game.sdk.api.common.User;
import com.kugou.game.sdk.base.BaseSDKWorkerFragment;
import com.kugou.game.sdk.core.UserManager;
import com.kugou.game.sdk.interfaces.OnRequestListener;
import com.kugou.game.sdk.ui.activity.CommonSdkActivity;
import com.kugou.game.sdk.ui.dialog.BaseConfirmDialog;
import com.kugou.game.sdk.ui.widget.LoadingView;
import com.kugou.game.sdk.utils.Rx;

import android.app.Activity;
import android.os.Bundle;
import android.os.Message;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

/**
 * 描述:修改支付密码
 *
 * @author jerryliu
 * @since 2015年8月20日 下午12:02:19
 */
public class SecurityPsdModifyFragment extends BaseSDKWorkerFragment {
    protected static final int MSG_BACK_MODIFY_PAY_PSD = 11;

    protected static final int MSG_UI_MODIFY_PAY_PSD_SUCCESS = 12;

    protected static final int MSG_UI_MODIFY_PAY_PSD_FAILE = 13;

    private Activity context;

    private TextView tvAccount, tvOldPsdInputErrorTip, tvNewPayPsdInputErrorTip,
            tvNewPayPsdInputErrorTip2, tvForgetPayPsd;

    private EditText etOldPayPsd, etNewPayPsd, etNewPayPsdConfirm;

    private Button btnConfirm;

    private LoadingView loadingView;

    private View layoutContent;

    private User user;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = getActivity();
        user = UserManager.getInstance().getCurrentUserInternal();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(Rx.layout.kg_fragment_security_psd_modify_, null);
        initView(view);
        return view;
    }

    private void initView(View view) {
        loadingView = (LoadingView) view.findViewById(Rx.id.kg_loadingView);
        loadingView.setText("正在操作，请稍候…");
        layoutContent = view.findViewById(Rx.id.kg_layout_bind_phone);
        tvAccount = (TextView) view.findViewById(Rx.id.kg_tv_username);
        tvAccount.setText(user.getAccountName());

        tvOldPsdInputErrorTip = (TextView) view.findViewById(Rx.id.kg_input_error_tip_1);
        tvNewPayPsdInputErrorTip = (TextView) view.findViewById(Rx.id.kg_input_error_tip_2);
        tvNewPayPsdInputErrorTip2 = (TextView) view.findViewById(Rx.id.kg_input_error_tip_3);
        tvForgetPayPsd = (TextView) view.findViewById(Rx.id.kg_tv_forget_password);
        tvForgetPayPsd.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (getActivity() != null && !getActivity().isFinishing()) {
                    context.startActivity(CommonSdkActivity.creatIntent(context,
                            SecurityPsdResetFragment.class.getName(),
                            Rx.string.kg_reset_security_psd));

                    getActivity().finish();
                }
            }
        });

        etOldPayPsd = (EditText) view.findViewById(Rx.id.kg_et_psd);
        etNewPayPsd = (EditText) view.findViewById(Rx.id.kg_et_phone_number);
        etNewPayPsdConfirm = (EditText) view.findViewById(Rx.id.kg_et_auth_code);
        btnConfirm = (Button) view.findViewById(Rx.id.kg_btn_confirm);
        btnConfirm.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                hideSoftInput(context);
                if (!NetWorkUtil.isNetworkAvailable(context)) {
                    showToast("请检查您的网络连接");
                    return;
                }

                String oldPayPsd = etOldPayPsd.getText().toString().trim();
                if (TextUtils.isEmpty(oldPayPsd) || oldPayPsd.length() != 6) {
                    showToast("请输入6位旧支付密码");
                    tvOldPsdInputErrorTip.setText("请输入6位旧支付密码");
                    tvOldPsdInputErrorTip.setVisibility(View.VISIBLE);
                    return;
                } else {
                    tvOldPsdInputErrorTip.setVisibility(View.GONE);
                }

                String newPayPsd = etNewPayPsd.getText().toString().trim();
                if (TextUtils.isEmpty(newPayPsd) || newPayPsd.length() != 6) {
                    showToast("请输入6位新支付密码");
                    tvNewPayPsdInputErrorTip.setText("请输入6位新支付密码");
                    tvNewPayPsdInputErrorTip.setVisibility(View.VISIBLE);
                    return;
                } else {
                    tvNewPayPsdInputErrorTip.setVisibility(View.GONE);
                }

                String newPayPsdConfirm = etNewPayPsdConfirm.getText().toString().trim();
                if (TextUtils.isEmpty(newPayPsdConfirm)) {
                    showToast("请再次输入新支付密码");
                    tvNewPayPsdInputErrorTip2.setText("请再次输入新支付密码");
                    tvNewPayPsdInputErrorTip2.setVisibility(View.VISIBLE);
                    return;
                } else {
                    tvNewPayPsdInputErrorTip2.setVisibility(View.GONE);
                }

                if (!newPayPsd.equals(newPayPsdConfirm)) {
                    showToast("您两次输入的新支付密码不相同");
                    tvNewPayPsdInputErrorTip2.setText("您两次输入的新支付密码不相同");
                    tvNewPayPsdInputErrorTip2.setVisibility(View.VISIBLE);
                    return;
                } else {
                    tvNewPayPsdInputErrorTip2.setVisibility(View.GONE);
                }

                loadingView.setVisibility(View.VISIBLE);
                layoutContent.setVisibility(View.GONE);
                sendEmptyBackgroundMessage(MSG_BACK_MODIFY_PAY_PSD);
            }
        });
    }

    @Override
    public void handleBackgroundMessage(Message msg) {
        final Message message = new Message();
        String username = user.getAccountName();
        String kgToken = user.getKugouToken();
        String oldPayPassword = etOldPayPsd.getText().toString().trim();
        String newPayPassword = etNewPayPsd.getText().toString().trim();
        switch (msg.what) {
            case MSG_BACK_MODIFY_PAY_PSD:
                UserManager.getInstance().updatePayPsd(username, kgToken, oldPayPassword,
                        newPayPassword, new OnRequestListener() {

                            @Override
                            public void onSuccess() {
                                message.what = MSG_UI_MODIFY_PAY_PSD_SUCCESS;
                            }

                            @Override
                            public void onFailed(String errorMsg) {
                                message.what = MSG_UI_MODIFY_PAY_PSD_FAILE;
                                message.obj = errorMsg;
                            }
                        });
                break;

        }
        sendUiMessage(message);
    }

    @Override
    public void handleUiMessage(Message msg) {
        if (getActivity() == null || getActivity().isFinishing() || isDetached() || !isAdded()) {
            return;
        }
        loadingView.setVisibility(View.GONE);
        layoutContent.setVisibility(View.VISIBLE);
        switch (msg.what) {
            case MSG_UI_MODIFY_PAY_PSD_SUCCESS:
                showSuccessDialog();
                break;
            case MSG_UI_MODIFY_PAY_PSD_FAILE:
                if (msg.obj != null) {
                    showToast(msg.obj.toString());
                }
                break;
        }

    }

    private void showSuccessDialog() {
        final BaseConfirmDialog dialog = new BaseConfirmDialog(context);
        dialog.setMessage("支付密码修改成功，请妥善保存以保证财产安全。");
        dialog.setSuccessLogoVisibility(View.VISIBLE);
        dialog.setDialogTitle("提示");
        dialog.setCancelBtnVisibility(View.GONE);
        dialog.setCanceledOnTouchOutside(false);
        dialog.setOKBtnText("知道了");
        dialog.setOkBtnBackground(Rx.drawable.kg_selector_bottom_single_filleted_corner_retangle);
        dialog.setCancelable(false);
        dialog.setOKClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                dialog.dismiss();
                if (getActivity() != null && !getActivity().isFinishing()) {
                    getActivity().finish();
                }
            }
        });
        dialog.show();
    }
}
